home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / pascal / dlgdsn41.zip / READSCPT.CPP < prev    next >
C/C++ Source or Header  |  1994-01-07  |  8KB  |  355 lines

  1. #define Uses_TStringCollection
  2.  
  3. //define NoBlink to disable Turbo Vision's screen clear at termination
  4. //if black flash is annoying.
  5. #ifdef NoBlink
  6. #define Uses_TScreen
  7. #endif
  8.  
  9. #include <tv.h>
  10. #include <stdlib.h>
  11. #include <dos.h>
  12. #include <dir.h>
  13. #include <fcntl.h>
  14. #include <string.h>
  15. #include <iostream.h>
  16. #include <fstream.h>
  17. #include <stdio.h>
  18. #include "readscpt.h"
  19.  
  20. #pragma warn -sig
  21.  
  22. #define MAXBUFF 350
  23. char *ch, buff[MAXBUFF];
  24. int lineNo;
  25. ifstream *inf;
  26. TNSCollection *ScriptColl;   //holds all the controls read
  27. ViewObj *Dialog;             //hols the dialog itself
  28. Boolean present[ScrollB + 1];  //tells which Kinds are present
  29. TStringCollection *classCollection;  //a collection of all the class names
  30.  
  31. void getch();
  32. long getNumber();
  33. char* getString();       //never NULL
  34. void skipWhiteSpace();
  35. void error(const char *S);
  36.  
  37. ViewObj::ViewObj()
  38. //the base struct constructor.  Read all the field common to all controls and dialog.
  39. {
  40.  BaseObj = getString();
  41.  Obj = getString();
  42.  X1 = getNumber();
  43.  Y1 = getNumber();
  44.  X2 = getNumber();
  45.  Y2 = getNumber();
  46.  DefOptns = getNumber() & ~0x1000; //make sure version 2 bit isn't present
  47.  Optns = getNumber() & ~0x1000;
  48.  DefEvMsk = getNumber();
  49.  EvMsk = getNumber();
  50.  HCtx = getNumber();
  51.  Grow = getNumber();
  52.  for (int i = 0; i < MAXPARAM; i++)
  53.    Param[i] = getString();
  54.  HelpCtxSym = getString();
  55.  FieldName = getString();
  56.  VarName = getString();
  57. }
  58.  
  59. DialogObj::DialogObj() : ViewObj()
  60. //dialog constructor. read fields special to the dialog.
  61. {
  62.  Palette  = getNumber();
  63.  WinFlags  = getNumber();
  64.  DlgFuncName  = getString();
  65.  KeyString = getString();
  66.  Title  = getString();
  67. }
  68.  
  69. ButtonObj::ButtonObj() : ViewObj()
  70. {
  71.  CommandName = getString();
  72.  ButtonText = getString();
  73.  CommandValue = getNumber();
  74.  Flags = getNumber();
  75. }
  76.  
  77. LabelObj::LabelObj() : ViewObj()
  78. {
  79.   LabelText = getString();
  80.   LinkName = getString();
  81. }
  82.  
  83. StaticTextObj::StaticTextObj() : ViewObj()
  84. {
  85.   Attrib = getNumber();
  86.   Text = getString();
  87. }
  88.  
  89. HistoryObj::HistoryObj() : ViewObj()
  90. {
  91.   HistoryID = getNumber();
  92.   HistoryLink = getString();
  93. }
  94.  
  95. InputLongObj::InputLongObj() : ViewObj()
  96. {
  97.   LongLabelText = getString();
  98.   LongStrLeng = getNumber();
  99.   LLim = getNumber();
  100.   ULim = getNumber();
  101.   ILOptions = getNumber();
  102. }
  103.  
  104. ListBoxObj::ListBoxObj() : ViewObj()
  105. {
  106.   Columns = getNumber();
  107.   ScrollBar = getString();
  108. }
  109.  
  110. MemoObj::MemoObj() : ViewObj()
  111. {
  112.   TextFieldName = getString();
  113.   BufSize = getNumber();
  114.   VScroll = getString();
  115.   HScroll = getString();
  116. }
  117.  
  118. ClusterObj::ClusterObj() : ViewObj()
  119. { int i;
  120.   Items = getNumber();
  121.   Mask = getNumber();
  122.   if (Items > 0) {
  123.     LabelColl = new TStringCollection(10,10);
  124.     for (i = 0; i < Items; i++)
  125.       LabelColl->atInsert(i, getString());   //entered in received order.
  126.                                              //prevent sorting
  127.     }
  128. }
  129. MultiCheckBoxObj::MultiCheckBoxObj() : ClusterObj()
  130. {
  131.   MCBFlags = getNumber();
  132.   SelRange = getNumber();
  133.   States = getString();
  134. }
  135.  
  136.  
  137.      PictureValidatorObj::PictureValidatorObj() : ValidatorObj()
  138.      {
  139.        AutoFill = getNumber();
  140.        PictureString = getString();
  141.      }
  142.  
  143.      RangeValidatorObj::RangeValidatorObj() : ValidatorObj()
  144.      {
  145.        LowLim = getNumber();
  146.        UpLim = getNumber();
  147.        Transfer = getNumber();
  148.      }
  149.  
  150.      FilterValidatorObj::FilterValidatorObj() : ValidatorObj()
  151.      {
  152.        CharSet = getString();
  153.        for (int i = 0; i <= 7; i++)
  154.          ActualCharSet[i] = getNumber();
  155.      }
  156.  
  157.      StringLookupValidatorObj::StringLookupValidatorObj() : ValidatorObj()
  158.      {
  159.       List = getString();
  160.      }
  161.  
  162. InputLineObj::InputLineObj() : ViewObj()
  163. {
  164.   StringLeng = getNumber();
  165.   valType ValKind = (valType)getNumber();
  166.   ValPtrName = getString();
  167.   switch (ValKind)  {
  168.     case Picture: val = new PictureValidatorObj(); break;
  169.     case Range:   val = new RangeValidatorObj();   break;
  170.     case Filter:  val = new FilterValidatorObj();  break;
  171.     case StringLookup: val = new StringLookupValidatorObj(); break;
  172.     }
  173. }
  174.  
  175. char* myNewStr(const char* S)
  176. //like newStr but never returns a NULL pointer
  177. {
  178.  char *P;
  179.  if (S[0] == '\0') {
  180.     P = new char[ 1 ];     //kind of silly, but saves a lot of testing here
  181.     *P = '\0';
  182.    }
  183.  else P = newStr(S);
  184.  return P;
  185. }
  186.  
  187. void getch()    //read next character in script file
  188. {
  189.  if (*ch == '\0') {     //need to read a line
  190.     if (!inf->eof()) {
  191.       inf->getline(buff, 255);
  192.       lineNo++;
  193.       ch = buff;
  194.       }
  195.     else error("Unexpected end of file");
  196.    }
  197.  else ch++;
  198. }
  199.  
  200. void spaces(int n)
  201. {for (int i = 1; i <= n; i++)
  202.   cout << ' ';
  203. }
  204.  
  205. void error(const char *S)  //handles error reporting
  206. {
  207.  short X;
  208.  char newS[80], tmp[20];
  209.  
  210.  cout << buff << endl;
  211.  X = ch-buff-1;
  212.  if (X < 1) X = 1;
  213.  strcpy(newS, "Line ");
  214.  itoa(lineNo, tmp, 10);
  215.  strcat(newS, tmp);
  216.  strcat(newS, " Error, ");
  217.  strncat(newS, S, 79-strlen(newS));
  218.  if (X > strlen(newS)) {
  219.    spaces(X-strlen(newS)-1);
  220.    cout << newS << '^' <<endl;
  221.    }
  222.  else {
  223.    spaces(X-1);
  224.    cout << '^' << newS << endl;
  225.    }
  226.  inf->close();
  227.  exit(1);
  228. }
  229.  
  230. void checkMemory()
  231. {
  232.  if (lowMemory()) {
  233.    cout << "Out of memory\n";
  234.    exit(1);
  235.    }
  236. }
  237.  
  238. void skipWhiteSpace()
  239. {
  240.  char c = *ch;
  241.  while (c == ' ' || c == '\t' || c == '\0')  {
  242.    getch();
  243.    c = *ch;
  244.    }
  245. }
  246.  
  247. char* getString()
  248. //reads a string in double quotes "like this \"one\"".  Never returns NULL
  249. {
  250.  char S[MAXBUFF] = "";
  251.  int i = 0;
  252.  skipWhiteSpace();
  253.  if (*ch != '\"')
  254.    error("Quoted string expected");
  255.  getch();
  256.  while ((*ch != '\"' || ch[1] == '+') && i < MAXBUFF-3) {
  257.    if (ch[0] == '\\' && ch[1] =='\"') {
  258.      S[i++] = '\\';
  259.      S[i++] = '\"';
  260.      getch();        //use up the extra character
  261.      }
  262.    else if (ch[0] == '\"' && ch[1] == '+') {  // a string continuation
  263.      getch();  //skip '"'
  264.      getch();  //skip '+'
  265.      skipWhiteSpace();
  266.      if (*ch != '\"')
  267.         error("Quoted string continuation expected");
  268.      }
  269.    else S[i++] = *ch;  //Normal case
  270.    getch();
  271.    }
  272.  getch();    //use up last "
  273.  S[i] = '\0';
  274.  return myNewStr(S);  //getString is never NULL
  275. }
  276.  
  277. long getNumber()  //reads a decimal number
  278. {
  279.  char S[20] = "";
  280.  int i = 0;
  281.  skipWhiteSpace();
  282.  if (*ch == '-') {
  283.    S[i++] = '-';
  284.    getch();
  285.    }
  286.  if (*ch < '0' || *ch > '9')
  287.    error("Number expected");
  288.  while (*ch >= '0' && *ch <= '9' && i < 20) {
  289.    S[i++] = *ch;
  290.    getch();
  291.    }
  292.  return atol(S);
  293. }
  294.  
  295. void readScriptFile(char* scriptName)
  296. {
  297. #ifdef NoBlink
  298. //disable clearScreen() by inserting a Retf instruction
  299.  *(uchar*) TScreen::clearScreen = 0xCB;
  300. #endif
  301.  
  302.  inf = new ifstream(scriptName);
  303.  if (!inf->good()) {
  304.    cout << "Can't open script file, " << scriptName << endl;
  305.    inf->close();
  306.    exit(1);
  307.    }
  308.  
  309.  char tmp[10];
  310.  inf->getline(tmp, 10);
  311.  if (strcmp(VersionID, tmp) != 0) {
  312.    cout << scriptName << " is not a valid script file\n";
  313.    inf->close();
  314.    exit(1);
  315.    }
  316.  
  317.  lineNo = 1;
  318.  buff[0] = '\0';  //start the reading
  319.  ch = buff;
  320.  getch();
  321.  getString();    // reserved
  322.  getNumber();    //Field number--of no use here
  323.  
  324.  ScriptColl = new TNSCollection(10,10); //start collection for the controls
  325.  classCollection = new TStringCollection(10,10);
  326.  
  327.  recType Kind;
  328.  
  329.  Kind = (recType)getNumber();
  330.  if (Kind != Dlg)
  331.    error("First item is not TDialog type");
  332.  Dialog = getKind(Kind);
  333.  classCollection->insert(newStr(Dialog->Obj));
  334.  present[Kind] = True;
  335.  skipWhiteSpace;
  336.  
  337.  ViewObj *P;
  338.  Kind = (recType)getNumber();
  339.  while (Kind != Done)  {
  340.    checkMemory();
  341.    //getKind must be defined elsewhere.  It returns the final ViewObj extension
  342.    //appropriate for Kind.
  343.    P = getKind(Kind);
  344.    if (P == 0)
  345.      error("Unrecognized control type");
  346.    ScriptColl->insert(P);
  347.    ccIndex i;
  348.    if (!classCollection->search(P->Obj, i))
  349.        classCollection->insert(newStr(P->Obj));
  350.    present[Kind] = True;   //keep track of which types are present
  351.    skipWhiteSpace();
  352.    Kind = (recType)getNumber();
  353.    }
  354. }
  355.